import 'dart:async';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ugrownews_flutter/main/ScreenMain.dart';
import 'package:ugrownews_flutter/utils/RouterUtil.dart';

class LoadScreen extends StatefulWidget {
  @override
  _LoadScreenState createState() => _LoadScreenState();
}

class _LoadScreenState extends State<LoadScreen> {
  int _step = 3;
  Timer timer;

  @override
  void initState() {
    super.initState();
    //倒计时三秒
    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      _step--;
      if (_step < 0) {
        //启动新页面，并退出当前页
        RouterUtil.getInstance().navigateTo(context,RouterUtil.ROUTER_MAIN,replace: true,transition: TransitionType.cupertino);
        return;
      } else {
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    //初始化屏幕适配

    ScreenUtil.instance = ScreenUtil(width: 1080, height: 1920)..init(context);
    return new Stack(
      alignment: Alignment.topRight,
      children: <Widget>[
        new Image.asset(
          "images/load.png",
          width: ScreenUtil.screenWidthDp,
          height: ScreenUtil.screenHeightDp,
          fit: BoxFit.fill,
        ),
        new Positioned(
            top: MediaQuery.of(context).padding.top +
                ScreenUtil.instance.setWidth(30),
            right: ScreenUtil.instance.setWidth(30),
            child: new GestureDetector(
              onTap: () {
                RouterUtil.getInstance().navigateTo(context,RouterUtil.ROUTER_MAIN,replace: true);
              },
              child: new Text("跳过 $_step"),
            ))
      ],
    );
  }

  @override
  void dispose() {
    //销毁计时器
    if (!(timer == null)) {
      if (timer.isActive) {
        timer.cancel();
      }
      timer = null;
    }
    super.dispose();
  }
}
